package com.hc.server.actions;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

/**
 * 获取活动信息
 * @author xujh
 *
 */
public class QUERY_ACTS implements IAction {

	private static final String CITY = "CITY";

	private static final String SN = "SN";
//	private static final String NAME = "NAME";
	private static final String PIC = "PIC";//图片地址
//	private static final String ADD = "ADD";//费用包含
//	private static final String USAGE = "USAGE";//使用说明
//	private final static String STOCK = "STOCK";//库存
//	private final static String SOLD = "SOLD";//已经销售
//	private static final String DES = "DES";//描述
//	private final static String MPRICE = "MPRICE";//市场价格
//	private static final String SPRICE = "SPRICE";//销售价格
	
	
	private static final String ACTS = "ACTS";
	
//	private final static String MALL_TOTAL_COUNT = "totalCount";
//	private final static String MALL_ = "rowsOfPage";
//	private final static String MALL_ = "totalPage";
//	private final static String MALL_ = "currentPage";
	private final static String MALL_LIST = "list";
	
//	private final static String MALL_NAME = "name";
	private final static String MALL_PIC = "picUri";
	private final static String MALL_SN = "pdtSn";
//	private final static String MALL_ADD = "address";
//	private final static String MALL_USAGE = "useExplain";
//	private static final String MALL_STOCK = "stockCount";
//	private static final String MALL_SOLD = "saleCount";
//	private static final String MAll_MARKETPRICE = "marketPrice";
//	private static final String MALL_SALEPRICE = "localPrice";
//	private static final String MALL_DES = "brief";
	//private final static String TOTAL_RESULT = "TOTAL_RESULT";
	
	@Override
	public void execute(Request req, Response resp) {
		//FIXME:活动尽量控制在6条之内
		String city;
		try {
			city = req.getStrArg(CITY);
		} catch (JSONException e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		//
		MallResponse mResp = null;
		try{
			mResp = MallServerHelper.queryActivities(city, "", 1, 6);
		}catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		}
		if(0 != mResp.getRetCode()){
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}
		
		try{
			JSONObject result = mResp.getResult();
			JSONArray activities = new JSONArray();
			JSONArray list = result.getJSONArray(MALL_LIST);//应该使用get方式
			if(0 != list.length()){
				for(int index = 0; index < list.length(); index++){
					JSONObject tmp = list.getJSONObject(index);
					JSONObject activity = new JSONObject();
					
					activity.put(SN, tmp.getString(MALL_SN));
					activity.put(PIC, tmp.getString(MALL_PIC));
					
					activities.put(activity);
				}
			}
			resp.appendRespContent(ACTS, activities);
		}catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}

		
		
		
	}

}
